﻿using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace CMS.Common.Validation
{
    public class WebsiteAttribute : RegularExpressionAttribute, IClientValidatable
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="WebsiteAttribute"/> class.
        /// </summary>
        public WebsiteAttribute()
            : base(@"^((http|https|ftp)\://)?[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&amp;%\$#\=~])*[^\.\,\)\(\s]$")
        {
        }

        /// <summary>
        /// When implemented in a class, returns client validation rules for that class.
        /// </summary>
        /// <param name="metadata">The model metadata.</param>
        /// <param name="context">The controller context.</param>
        /// <returns>
        /// The client validation rules for this validator.
        /// </returns>
        public System.Collections.Generic.IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var rule = new ModelClientValidationRegexRule(ErrorMessage, Pattern);
            rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName());
            return new[] { rule };
        }
    }
}